import React, { Component } from 'react'

export default class New extends Component {
  constructor () {
    super()

    console.log('---New---constructor')

    this.state = {
      name: '刘腾'
    }
  }

  static getDerivedStateFromProps (props, state) {
    console.log('---News---getDerivedStateFromProps---', props, state)

    // 视图中到底渲染啥，是由你这里决定的
    // return {
    //   name: '张三'
    // }

    // return state
    return null
  }

  shouldComponentUpdate () {
    console.log('---New---shouldComponentUpdate')
    return true
  }

  render () {
    console.log('---New---render')
    return (
      <div>
        New
        <p>我曾经叫做 {this.state.name}</p>
        <p>count is {this.props.count}</p>
        <p>
          <button onClick={() => this.setState({ name: '刘腾飞' })}>
            change
          </button>
        </p>
        <p>
          <button onClick={() => this.forceUpdate()}>强制更新</button>
        </p>
      </div>
    )
  }

  getSnapshotBeforeUpdate (prevProps, prevState) {
    console.log('---getSnapshotBeforeUpdate---', prevProps, prevState)
    return {
      money: 1000000000
    }
  }

  componentDidMount () {
    console.log('---News---componentDidMount---')
  }

  componentDidUpdate (prevProps, prevState, snapshot) {
    console.log('---componentDidUpdate---', prevProps, prevState, snapshot)
  }

  componentWillUnmount () {
    console.log('---News---componentWillUnmount---')
  }
}
